#ifndef CPublicData_H
#define CPublicData_H
/********************************************************************************
** @ Date:2025/05/19
** @ Author: HanJiaXin
** @ Description:公共数据单例类
** @ Version:1.0.0
** @ ClassName:CPublicData
** @ Fun:
********************************************************************************/
#include <QString>
#include <QVector>
#include <QMutex>
#include "ctagdata.h"

class CClientData;

struct SServerCfg
{
    QString m_sServerIp;        //服务器监听Ip
    int m_nPort;                //服务器监听端口号
    int m_nClientMax;           //支持客户端最大连接数
    int m_nClientNum;           //客户端配置数
    quint8 m_nReplyByte0;       //服务端回复透传低位
    quint8 m_nReplyByte1;       //服务端回复透传高位
    int m_nTagTimeOut;          //测点刷新超时时间 单位秒

    SServerCfg()
    {
        m_sServerIp = "127.0.0.1";
        m_nPort = 0;
        m_nClientMax = 10;
        m_nClientNum = 1;
        m_nReplyByte0 = 0x00;
        m_nReplyByte1 = 0xff;
        m_nTagTimeOut = 900;
    }
};

//SyncBase连接信息
struct SSyncBaseCfg
{
    QString m_sIp;
    int m_nPort;
    QString m_sUser;
    QString m_sPassWd;
    QString m_sDbName;
    SSyncBaseCfg()
    {
        m_sIp = "127.0.0.1";
        m_nPort = 8007;
        m_sUser = "admin";
        m_sPassWd = "";
        m_sDbName = "NT6000";
    }
};

class CPublicData
{

public:
    //测点变化状态
    enum enumTagChanged
    {
        enTagNoChange = 0,  //测点无变化
        enTagReloading,     //测点重载中
        enTagUpDate,        //测点重载完成，需要更新
    };
public:
    static CPublicData& instance()
    {
        static CPublicData instance;
        return instance;
    }

    void reLoad();                      //重新加载
    void saveIni();                     //保存配置
    void loadIni();
    void loadTag();                     //加载测点点表
    void saveTag();                     //保存全部测点
    void reLoadClientTag(CClientData* pClient); //重载某个客户端的测点

private:
    void loadTagClient(CClientData* pClient);//加载某个客户端的点表
    void saveTag(const QString& sGuid); //按Guid保存测点

public:
    QMutex*                     m_pMutex;
    bool                        m_bAutoStart;           //是否自启动
    SSyncBaseCfg                m_stSyncBaseCfg;        //SyncBase配置
    SServerCfg                  m_stServerCfg;          //服务端配置
    bool                        m_bServerRuninng;       //服务运行状态

    QList<CClientData*>         m_listClient;           //客户端链表，保存客户端结构关系
    QMap<QString, CClientData*> m_mapClient;            //客户端字典，保存客户端guid对照关系
    QVector<CTagData*>          m_vecTag;               //测点列表
    QVector<CTagData*>          m_vecTagHistory;        //历史测点列表
    QString                     m_sCurVer;              //当前版本

    int                         m_nClientTagReload;     //客户端测点重载 见CPublicData::enumTagChanged

private:
    CPublicData();
    ~CPublicData();
};

#endif
